#include "file_module/file_errno.h"
#include "file_module/file_handle.h"
#include "hls_m3u8_message.h"
#include "hls_m3u8_parser.h"
#include "hls_protocol_errno.h"
#include "log_module/log_public_module.h"
#include "write_read_module/buffer.h"

#include <cstdint>
#include <cstdio>
#include <gtest/gtest.h>
#include <unistd.h>

TEST(HlsM3u8Parser, Init)
{
    std::string line   = "#EXT-X-TARGETDURATION:9";
    std::string prefix = "#EXT-X-TARGETDURATION:";
    HlsM3u8Parser parser;
    parser.ExtXTargetDuration(prefix, line);
}

TEST(HlsM3u8Parser, LineTest)
{
    std::string line   = "#EXTM3U\n#EXT-X-TARGETDURATION:9\n#EXT-X-VERSION:4\n#EXT-X-MEDIA-SEQUENCE:1\n#EXT-X-PLAYLIST-TYPE:VOD\n#EXTINF:9,\n14957-3_00000.ts\n#EXT-X-ENDLIST";
    HlsM3u8Parser parser;
    int use_length = 0;
    parser.Parser((const uint8_t*)line.data(), line.size(), use_length);
    std::string content;
    HlsM3U8MessageUtil::BuildM3U8Content(parser.HlsMessage(), "./", content);
    printf("%s.\n", content.c_str());
}

TEST(SplitField, Test)
{
    std::string content = R"(FRAME-RATE=29.970,BANDWIDTH=5431000,AVERAGE-BANDWIDTH=5158000,RESOLUTION=1280x720,CLOSED-CAPTIONS="cc1",CODECS="avc1.4d401e,mp4a.40.2")";
    std::map<std::string, std::string> result_map;
    HlsM3u8Parser::SplitField(content, result_map, ",");
    for (auto & item : result_map)
    {
        LOG_UNITTEST_MODULE("key:%s, value:%s", item.first.c_str(), item.second.c_str());
    }
}

TEST(HlsM3u8Parser, TestFile)
{
    std::string filename = "./../hls-index.m3u8";
    if (access(filename.c_str(), F_OK) >= 0)
    {
        HlsM3u8Parser   parser;
        FileHandle      file(filename);
        int             result = file.OpenFileOnlyRead();
        WriteReadBuffer buffer;
        while (result >= 0)
        {
            uint8_t * data = file.Data(result);
            if (result == 0)
            {
                break;
            }
            else
            {
                buffer.PushDataToBuffer(data, result);
                file.RemoveDataLength(result);
            }
        }
        printf("file length : %d.\n", buffer.BufferDataSize());
        if (buffer.BufferDataSize() > 0)
        {
            int length = 0;
            result     = parser.Parser(buffer.BufferData(), buffer.BufferDataSize(), length);
            printf("parser result : %d and length %d.\n", result, length);
            buffer.RemoveSize(length);
            if (result >= 0)
            {
                auto        msg = parser.HlsMessage();
                std::string c;
                HlsM3U8MessageUtil::BuildM3U8Content(msg, "", c);
                printf("%s.\n", c.c_str());
            }
        }
    }
    else
    {
        printf("no found file %s.\n", filename.c_str());
    }
}

TEST(ParserHlsFile, File)
{
    // https://nasaplus.akamaized.net/output/14941-7.m3u8
    std::string filename = "/home/huangjie/para/resource/hls_resource/14941-7.m3u8";
    FileHandle file_handle(filename, 1000);
    if (file_handle.OpenFileOnlyRead() == EmFileErrno::EmFileErrno_SUCCESS)
    {
        file_handle.OpenFileOnlyRead();
        HlsM3u8Parser parser = {};
        while (true)
        {
            int length     = 0;
            int use_length = 0;

            const uint8_t * data = file_handle.Data(length);
            int result = parser.Parser(data, length, use_length);
            LOG_UNITTEST_MODULE("result:%d,use length:%d,file data size:%lu", result, use_length, file_handle.GetFileSize());
            if (result != EmHlsProtocolErrno::EmHlsProtocolErrno_NEED_DATA)
            {
                if (result == EmHlsProtocolErrno::EmHlsProtocolErrno_SUCCESS)
                {
                    auto msg = parser.HlsMessage();
                    std::string content;
                    HlsM3U8MessageUtil::BuildM3U8Content(msg, "", content);
                    LOG_UNITTEST_MODULE("\n%s", content.c_str());
                }
                break;
            }
            file_handle.RemoveDataLength(use_length);
        }
    }
    else
    {
        printf("open file %s failed.\n", filename.c_str());
    }
}

TEST(ParserResolution, Test)
{
    // https://nasaplus.akamaized.net/output/14941.m3u8
    std::string file = "/home/huangjie/para/resource/hls_resource/14941.m3u8";
    FileHandle file_handle(file);
    file_handle.OpenFileOnlyRead();
    int length     = 0;
    int use_length = 0;
    const uint8_t * data = file_handle.Data(length);
    HlsM3u8Parser parser = {};
    int result = parser.Parser(data, length, use_length);
    LOG_UNITTEST_MODULE("result:%d,use length:%d,file data size:%lu", result, use_length, file_handle.GetFileSize());
}